<?php
/**
* @name Maiji
* @author pd87@qq.com
* @copyright m-j.cc
* @version v1.0
*/

class APP_RUN 
{
	public static function RUN(){
		global $_URL;
		$_controllera = 'APP\\'.ucwords($_URL[0]).'\\'.ucwords($_URL[1]);
		$_controllerb = ucwords($_URL[1]);
		$_controllerc = $_URL[2];
		$_controllerpath = APP_PATH.$_URL[0].'/controller/'.ucwords($_URL[1]).'.php';
		if(file_exists($_controllerpath)){
			require_once $_controllerpath;
			if(file_exists(APP_PATH.$_URL[0].'/function.php')){
				require_once APP_PATH.$_URL[0].'/function.php';
			}
			if(class_exists($_controllera,false)){
				$maiji = new $_controllera();
				$_ls = get_class_methods($maiji);
				if(in_array('_sys_ini_run',$_ls)){
					if(method_exists($maiji,$_controllerc)){
						$maiji -> _sys_ini_run($_URL);
						$maiji -> _sys_front_run();
						$maiji -> {$_controllerc}($_URL['page']);
						if(file_exists(APP_PATH.$_URL[0].'/view/'.$_URL[1].'/'.$_controllerc.C('basic.TPL_NAME'))){
							$maiji -> template_dir = APP_PATH . $_URL[0].'/view/'.$_URL[1].'/';
							$maiji -> display($_URL[1].'/'.$_controllerc);
						}
						$maiji -> _sys_after_run();
					}else{
						if(!C('basic.DEBUG')){
							MAIJI\Log::x('方法不存在:'.$_controllerpath."-----".$_controllera.'\\'.$_controllerc,'error');
							$_controllerpath = '这个位置怎么能告诉你呢! 嘿嘿!';
						}
						MAIJI\APP_Error::display('方法不存在 -> '.$_controllera.'\\'.$_controllerc,$_controllerpath);
					}
				}else{
					if(method_exists($maiji,$_controllerc)){
						$maiji -> {$_controllerc}($_URL['page']);
					}else{
						if(!C('basic.DEBUG')){
							MAIJI\Log::x('方法不存在:'.$_controllerpath."-----".$_controllera.'\\'.$_controllerc,'error');
							$_controllerpath = '这个位置怎么能告诉你呢! 嘿嘿!';
						}
						MAIJI\APP_Error::display('方法不存在 -> '.$_controllera.'\\'.$_controllerc,$_controllerpath);
					}
				}
			}else{
				$maiji = new $_controllerb();
				if(method_exists($maiji,$_controllerc)){
					$maiji -> {$_controllerc}($_URL['page']);
				}else{
					if(!C('basic.DEBUG')){
						MAIJI\Log::x('方法不存在:'.$_controllerpath."-----".$_controllera.'\\'.$_controllerc,'error');
						$_controllerpath = '这个位置怎么能告诉你呢! 嘿嘿!';
					}
					MAIJI\APP_Error::display('方法不存在 -> '.$_controllerb.'\\'.$_controllerc,$_controllerpath);
				}
			}
		}else{
			if(!C('basic.DEBUG')){
				MAIJI\Log::x('文件不存在：'.$_controllerpath,'error');
				$_controllerpath = '这个位置怎么能告诉你呢! 嘿嘿!';
			}
			MAIJI\APP_Error::display('文件找不到了 -> '.$_controllerb.'\\'.$_controllerc,$_controllerpath);
		}
	}
}